<template>
	<view>
		<view class="tips">
			<text>内容由人工智能生成，仅供参考</text>
		</view>
	</view>
</template>

<script setup>
import apis from '@/apis'
import axios from '@/apis/request'
import { useSettingStore } from '@/stores/setting'
import { onLoad } from '@dcloudio/uni-app'
import { storeToRefs } from 'pinia'
import { ref } from 'vue'

const userId = ref('')
const userName = ref('')
onLoad(params => {
	userId.value = params.userId
	userName.value = params.userName
	loadToken().then(chatInit)
})

const token = ref('')
const { setting } = storeToRefs(useSettingStore())

function loadToken() {
	return axios
		.get(apis.common.cozeToken)
		.then(res => {
			token.value = res
			return res
		})
		.catch(() => {
			uni.showModal({
				title: '提示',
				content: '鉴权失败，请重试！',
				showCancel: false,
				success: function (res) {
					if (res.confirm) {
						if (uni.webView) {
							uni.webView.navigateBack()
						}
					}
				}
			})
		})
}

function chatInit() {
	const cozeWebSDK = new CozeWebSDK.WebChatClient({
		config: {
			bot_id: setting.value.botId,
			isIframe: false
		},
		auth: {
			type: 'token',
			token: token.value,
			onRefreshToken: async () => {
				return await loadToken()
			}
		},
		userInfo: {
			id: userId.value,
			url: 'https://qn.base.file.gintell.cn/icon/jg/user.png',
			nickname: userName.value
		},
		ui: {
			base: {
				lang: 'zh-CN',
				icon: 'https://qn.base.file.gintell.cn/icon/jg/ai-logo.jpeg'
			},
			header: {
				isShow: true,
				isNeedClose: false
			},
			asstBtn: {
				isNeed: false
			},
			footer: {
				isShow: false
			},
			chatBot: {
				title: setting.value.agentName || '智能助理'
			}
		}
	})
	cozeWebSDK.showChatBot()
}
</script>

<style scoped lang="scss">
.tips {
	position: fixed;
	font-size: 24rpx;
	color: #666;
	top: 60rpx;
	right: 120rpx;
	z-index: 9000;
}
</style>
